<?php
namespace app\store\controller\apps\lottery;

use app\store\controller\Controller;
use app\store\model\lottery\Prize as PrizeModel;
/**
 * 
 */
class Prize extends Controller
{
	
	public function index()
	{
		$model = new PrizeModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
	}

	/**
     * 添加奖品
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        $model = new PrizeModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add', compact('model'));
        }
        if ($model->add($this->postData('prize'))) {
            return $this->renderSuccess('添加成功', url('apps.lottery.prize/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新奖品
     * @param $prize_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($prize_id)
    {
        // 模板详情
        $model = PrizeModel::get($prize_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('prize'))) {
            return $this->renderSuccess('更新成功', url('apps.lottery.prize/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }


    public function state($prize_id, $state)
    {
    	// 活动详情
        $model = PrizeModel::get($prize_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除奖品
     * @param $prize_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($prize_id)
    {
        $model = PrizeModel::get($prize_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }


}
?>